package com.voodoo.lib_frame.tip;

import android.app.Activity;
import android.content.Context;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.hjq.xtoast.XToast;
import com.voodoo.lib_frame.R;


/**
 * XToast 悬浮提示框的简单封装
 */
public class XToastUtils {

    XToast xToast;

    String msg = "";
    Handler handler = new Handler();
    private static XToastUtils xtoastUtils;

    public static XToastUtils getInstance() {
        if (xtoastUtils == null) {
            synchronized (XToastUtils.class) {
                if (xtoastUtils == null) {
                    xtoastUtils = new XToastUtils();
                }
            }
        }
        return xtoastUtils;
    }

    /**
     * 默认tipBitmap
     *
     * @param mContext
     * @param text
     * @param duration
     */
    public void toShowXToast(Context mContext, String text, int duration) {
        if (msg.equals(text)) {
            if (xToast != null && xToast.isShow()) {
                return;
            }
        }
        restShowView(duration);
        toXToastCancal();
        View view = addXToastView(mContext);
        TextView tv = view.findViewById(R.id.tips_msg);
        ImageView imgv = view.findViewById(R.id.tips_icon);
        imgv.setImageResource(R.drawable.icon_right_tip);
        tv.setText(text);
        msg = text;
        xToast.setDuration(duration).setDraggable()
                .setOutsideTouchable(true);
        xToast.show();


    }

    /**
     * 可切换的tipBitmap
     *
     * @param mContext
     * @param text
     * @param tipBitmap
     * @param duration
     */

    public void toShowXToast(Context mContext, String text, int tipBitmap, int duration) {
        if (msg.equals(text)) {
            if (xToast != null && xToast.isShow()) {
                return;
            }
        }
        restShowView(duration);
        toXToastCancal();
        View view = addXToastView(mContext);
        TextView tv = view.findViewById(R.id.tips_msg);
        ImageView imgv = view.findViewById(R.id.tips_icon);
        imgv.setImageResource(tipBitmap);
        tv.setText(text);
        msg = text;
        xToast.setDuration(duration).setDraggable()
                .setOutsideTouchable(true);
        xToast.show();

    }

    /**
     * 添加的view
     *
     * @param mContext
     */
    private View addXToastView(Context mContext) {

        View view;
        xToast = new XToast((Activity) mContext);
        LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = inflater.inflate(R.layout.view_toast_tip, null);
        xToast.setView(view);
        return view;
    }

    /**
     * 时间到了重置msg
     *
     * @param duration
     */
    public void restShowView(int duration) {
        if (handler != null) {
            handler.removeCallbacksAndMessages(null);
        }
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                msg = "";
            }
        }, duration);
    }

    /**
     * 取消释放 xToast
     */
    private void toXToastCancal() {

        if (xToast != null) {
            xToast.cancel();
            xToast.recycle();
        }
    }
}
